﻿using System;
using System.Collections.Generic;

using Gonte.Validation;

namespace Gonte
{
    /// <summary>
    /// Helper class to simplify throwing exceptions
    /// </summary>
    public static class ExceptionThrower
    {
        /// <summary>
        /// Throws an ArgumentNullException when the value is null
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void ThrowWhenNull(string name, Object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(name);
            }
        }

        /// <summary>
        /// Throws an ArgumentNullException when the value is null or empty
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void ThrowWhenNullOrEmpty(string name, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException(name);
            }
        }

        /// <summary>
        /// Throws an exception when the object is invalid
        /// </summary>
        /// <param name="object">Object to validate</param>
        public static void ThrowWhenNotValid(Object @object)
        {
            List<ValidationError> errors = new List<ValidationError>();

            if (!ObjectValidator.Validate(@object, ref errors))
            {
                throw new ValidationException(errors);
            }
        }

        /// <summary>
        /// Throws an invalid operation exception when the condition is true
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="message"></param>
        public static void ThrowWhen(bool condition, string message)
        {
            if (condition)
            {
                throw new InvalidOperationException(message);
            }
        }

        /// <summary>
        /// Throws an invalid operation exception when the condition is false
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="message"></param>
        public static void ThrowWhenNot(bool condition, string message)
        {
            if (!condition)
            {
                throw new InvalidOperationException(message);
            }
        }
    }
}
